<?php
/**
 * Created by PhpStorm.
 * User: 刘抱
 * Date: 2018/11/19
 * Time: 14:54
 */

namespace Mobile\Controller;


use Home\Controller\ContractBackController;
use Home\Services\CommonService;
use Home\Services\ContactService;
use Home\Services\CrmProjectsService;

class CrmProjectsController extends CommunalController
{
    //新运营首页
    public function index()
    {
        $where = array();
        $keyword = I('keyword');
        $my_project = I('my_project');
        if (!empty($my_project)) $where['add_uid'] = $_SESSION['uid'];
        if (!empty($keyword)) $where['project_num|project_name'] = array('like', '%' . $keyword . '%');
        $where['stage'] = 2;
        $res_projects = CrmProjectsService::getCrmProjectsArray('*', $where);
        $res_projects = CommonService::array_sort($res_projects, 'add_date');
        $this->assign('result', $res_projects);
        $this->assign('my_project', $my_project);
        $this->display();
    }

    //新运营详情页
    public function detail()
    {
        $project_id = I('project_id');
        $where['project_id'] = $project_id;
        $res_projects = CrmProjectsService::getCrmProjects('*', $where);//运营项目概况
        $this->assign('result', $res_projects);
        $this->display();
    }

    //运营周报
    public function weekly_list()
    {
        $project_id = I('project_id');
        $where['project_id'] = array('in', $project_id);
        $res_projects = CrmProjectsService::getCrmProjects('*', $where);//运营项目概况
        $this->assign('result', $res_projects);
        $res_weekly = CrmProjectsService::getWeeklyArray('*', $where, 'year desc,week desc');
        $this->assign('res_weekly', $res_weekly);
        $year = date('Y');
        $this->assign('week',date('W',time()));
        $this->assign('year',$year);
        $this->display();
    }

    //删除周报
    public function weekly_del()
    {
        $project_id = I('project_id');
        $where['weekly_id'] = I('weekly_id');
        $res = CrmProjectsService::weeklyDel($where);
        if (!empty($res)) {
            $this->success('周报删除成功', __APP__ . "Mobile/CrmProjects/weekly_list/project_id/$project_id");
        } else {
            $this->success('周报删除失败', __APP__ . "Mobile/CrmProjects/weekly_list/project_id/$project_id");
        }
    }

    //周报编辑
    public function weekly_edit()
    {
        $where['weekly_id'] = I('weekly_id');
        $res_weekly = CrmProjectsService::getWeekly('*', $where);
        $this->assign('res_weekly', $res_weekly);
        if (IS_POST == true) {
            try {
                $db = M();
                $db->startTrans();//开启事物
                $data = $_POST;
                $where_updata['weekly_id'] = $data['weekly_id'];
                $res = CrmProjectsService::updateWeekly($data, $where_updata);
                if (!empty($res)) {
                    $db->commit();//提交
                    $this->success('周报更新成功', __APP__ . "Mobile/CrmProjects/weekly_list/project_id/{$res_weekly['project_id']}");
                    die;
                } else {
                    $db->rollback();//回滚
                    $this->error('周报更新失败', __APP__ . "Mobile/CrmProjects/weekly_list/project_id/{$res_weekly['project_id']}");
                    die;
                }
            } catch (\Exception $exception) {
                $db->rollback();//回滚
                $this->error('周报更新失败', __APP__ . "Mobile/CrmProjects/weekly_list/project_id/{$res_weekly['project_id']}");
                die;
            }
        }
        $this->assign('weekly_id', I('weekly_id'));
        $this->display();
    }

    //周报添加
    public function weekly_add()
    {
        $where['pid'] = I('pid');
        $res_projects = CrmProjectsService::getCrmProjects('*', $where);
        $this->assign("year_list", $this->get_year_month_week(8, '', ''));
        $this->assign('year', date("Y"));
        $this->assign('result', $res_projects);
        if (IS_POST == true) {
            try {
                $db = M();
                $db->startTrans();//开启事物
                $data = $_POST;
                $res = CrmProjectsService::addWeekly($data);
                if (!empty($res)) {
                    $db->commit();//提交
                    $this->success('周报添加成功', __APP__ . "Mobile/CrmProjects/weekly_list/project_id/{$res_projects['project_id']}");
                    die;
                } else {
                    $db->rollback();//回滚
                    $this->error('周报添加失败', __APP__ . "Mobile/CrmProjects/weekly_list/project_id/{$res_projects['project_id']}");
                    die;
                }
            } catch (\Exception $exception) {
                $db->rollback();//回滚
                $this->error('周报添加失败', __APP__ . "Mobile/CrmProjects/weekly_list/project_id/{$res_projects['project_id']}");
                die;
            }
        }
        $this->display();
    }

    //周报详情
    public function weekly_detail()
    {
        $where['weekly_id'] = I('weekly_id');
        $res_weekly = CrmProjectsService::getWeekly('*',$where);
        $this->assign('res_weekly',$res_weekly);
        $this->display();
    }

    //收款信息查看
    public function back()
    {
        $project_id = I('project_id');
        $contractObj = new ContractBackController();
        $result = $contractObj->index('1', $project_id, null, null);
        $res_contract_back = ContractBackController::array_sort($result);//按指定字段重新排序
        $result_back = ContractBackController::array_new($res_contract_back, 'name');
        $this->assign('result_back', $result_back);
        $where['project_id'] = $project_id;
        $res_projects = CrmProjectsService::getCrmProjects('*', $where);//运营项目概况
        $this->assign('result', $res_projects);
        $this->display();
    }

    //运营项目联系人查看
    public function contact()
    {
        $where['project_id'] = I('project_id');
        $res_projects = CrmProjectsService::getCrmProjects('*', $where);//运营项目概况
        $this->assign('result', $res_projects);
        $where_contact['contact_id'] = $res_projects['main_linkman'];
        $res_contact = ContactService::getContact('*', $where_contact);
        $gender = array('1' => '男', '2' => '女');
        $this->assign('res_contact', $res_contact);
        $this->assign('gender', $gender);
        $job_level = $this->get_constant(C("JOB_LEVEL"));
        $relation = $this->get_constant(C("DECISION_RELATION"));
        $this->assign('job_level', $job_level);
        $this->assign('relation', $relation);
        $this->display();
    }

}